package com.lzx.iteam.net;

import android.os.Message;
import android.util.Log;
import com.lzx.iteam.CloudDialerActivity;
import com.lzx.iteam.bean.CloudContact;
import com.lzx.iteam.bean.PhoneNumItem;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by shijunting on 15-5-13.
 */
public class CloudSearchMsg  extends CcMsgStructure {

    private Message mmCallback;

    public CloudSearchMsg(Message callback) {
        mmCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {
        try {
            ArrayList<CloudContact> contactList = new ArrayList<CloudContact>();
            JSONObject jData = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);

            JSONArray jContactList = jData.getJSONArray(AsynHttpClient.KEY_RESULT);
            for (int i = 0; i < jContactList.length(); i++) {
                JSONObject jContact = jContactList.getJSONObject(i);

                CloudContact contact = new CloudContact();
                if(jContact.has(AsynHttpClient.KEY_GROUP_ID)){
                    contact.groupId = jContact.getString(AsynHttpClient.KEY_GROUP_ID);
                }
                contact.keyStartLocation = jContact
                        .getInt(AsynHttpClient.KEY_KEY_START_LOCATION);
                contact.keyCount = jContact.getInt(AsynHttpClient.KEY_KEY_COUNT);
                contact.keyType = jContact.getInt(AsynHttpClient.KEY_KEY_TYPE);
                contact.userId = jContact.getString(AsynHttpClient.KEY_USER_ID);//add by sjt
                contact.contactName = jContact.getString(AsynHttpClient.KEY_CONTACT_NAME);
//                    contact.contactUrl = jContact.getString(AsynHttpClient.KEY_CONTACT_URL);
//                contact.contactRemarks = jContact.getString(AsynHttpClient.KEY_CONTACT_REMARKS);
                contact.contactId = jContact.getString(AsynHttpClient.KEY_CONTACT_ID);
//                    contact.contactedCount = jContact.getInt(AsynHttpClient.KEY_CONTACT_DIALCOUNT);
                contact.contact_image = jContact.getString(AsynHttpClient.KEY_CONTACT_IMAGE);
//                contact.contact_status = jContact.getString(AsynHttpClient.KEY_CONTACT_STATUS);
//                String jNumListStr = jContact.getString(AsynHttpClient.KEY_CONTACT_PHONENUMBER);
//                JSONArray jNumListArr = new JSONArray(jNumListStr);
//                contact.contactPhoneNum = new ArrayList<PhoneNumItem>();
//                for (int j = 0; j < jNumListArr.length(); j += 2) {
//                    PhoneNumItem phoneItem = new PhoneNumItem();
//                    phoneItem.label = jNumListArr.getString(j);
//                    phoneItem.phoneNum = jNumListArr.getString(j + 1);
//                    contact.contactPhoneNum.add(phoneItem);
//                }

//                Log.i("contact_status", contact.contact_status);
                contactList.add(contact);
            }

            if (CloudDialerActivity.sCloudSearch == CloudDialerActivity.CLOUD_SEARCH_PENDING_SEND) {
                return;
            } else {
                CloudDialerActivity.sCloudSearch = CloudDialerActivity.CLOUD_SEARCH_IDLE;
            }

            if (mmCallback != null) {
                mmCallback.obj = contactList;
                mmCallback.sendToTarget();
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (mmCallback != null) {
            mmCallback.arg1 = -1;
            mmCallback.sendToTarget();
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}
